﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.Util;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 添加字段
    /// </summary>
    public partial class FrmField : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmField()
        {
            InitializeComponent();           
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAddField_Load(object sender, EventArgs e)
        {
            AddTypes();
            if (NewField != null)
            {
                txtName.ReadOnly = true;
                cmbType.Enabled = false;
                ShowProperty();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public IField NewField { get; set; }
        
        /// <summary>
        /// 添加数据类型
        /// </summary>
        private void AddTypes()
        {
            List<ValueName> values = new List<ValueName>()
            {
                new ValueName(){Name="Short Integer", Value =esriFieldType.esriFieldTypeSmallInteger.ToString()},
                new ValueName(){Name="Long Integer", Value =esriFieldType.esriFieldTypeInteger.ToString()},
                new ValueName(){Name="Float", Value =esriFieldType.esriFieldTypeSingle.ToString()},
                new ValueName(){Name="Double", Value =esriFieldType.esriFieldTypeDouble.ToString()},
                new ValueName(){Name="Text", Value =esriFieldType.esriFieldTypeString.ToString()},
                new ValueName(){Name="Date", Value =esriFieldType.esriFieldTypeDate.ToString()},
                new ValueName(){Name="Blob", Value =esriFieldType.esriFieldTypeBlob.ToString()},
                //new ValueName(){Name="Raster", Value =esriFieldType.esriFieldTypeRaster.ToString()},
                new ValueName(){Name="Guid", Value =esriFieldType.esriFieldTypeGUID.ToString()}
            };
            if (NewField != null)
            {
                values.Add(new ValueName() { Name = "Geometry", Value = esriFieldType.esriFieldTypeGeometry.ToString() });
                values.Add(new ValueName() { Name = "Object ID", Value = esriFieldType.esriFieldTypeOID.ToString() });
            }
            cmbType.ValueMember = "Value";
            cmbType.DisplayMember = "Name";
            cmbType.DataSource = values;
            cmbType.SelectedIndex = 0;
        }

        /// <summary>
        /// 显示字段信息
        /// </summary>
        private void ShowProperty()
        {
            txtName.Text = NewField.Name;
            txtAlias.Text = NewField.AliasName;
            cmbType.SelectedValue = NewField.Type.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            esriFieldType seltype = (esriFieldType)Enum.Parse(typeof(esriFieldType), cmbType.SelectedValue.ToString());
            switch (seltype)
            {
                case esriFieldType.esriFieldTypeString:
                    advPropertyGrid1.SelectedObject = new FieldString();
                    break;
                case esriFieldType.esriFieldTypeRaster:
                    advPropertyGrid1.SelectedObject = new FieldRaster();
                    break;
                case esriFieldType.esriFieldTypeBlob:
                    advPropertyGrid1.SelectedObject = new FieldBlob();
                    break;
                default :
                    advPropertyGrid1.SelectedObject = new FieldEdit();
                    break;
            };
        }


        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtName.Text)) return;
            esriFieldType seltype = (esriFieldType)Enum.Parse(typeof(esriFieldType), cmbType.SelectedValue.ToString());
            if (NewField == null)
                NewField = new Field();
            IFieldEdit fd = NewField as IFieldEdit;
            fd.Name_2 = txtName.Text.Trim();
            fd.AliasName_2 = string.IsNullOrWhiteSpace(txtAlias.Text) ? txtName.Text.Trim() : txtAlias.Text.Trim();
            fd.Type_2 = seltype;
            switch (seltype)
            {
                case esriFieldType.esriFieldTypeString:
                    FieldString ts = (FieldString)advPropertyGrid1.SelectedObject;
                    fd.IsNullable_2 = ts.AllowNull;
                    fd.DefaultValue_2 = ts.DefaultValue;
                    fd.Length_2 = ts.Length;
                    break;
                case esriFieldType.esriFieldTypeRaster:
                    //FieldRaster tr = (FieldRaster)advPropertyGrid1.SelectedObject;
                    break;
                case esriFieldType.esriFieldTypeBlob:
                    FieldBlob tb = (FieldBlob)advPropertyGrid1.SelectedObject;
                    fd.IsNullable_2 = tb.AllowNull;
                    fd.Length_2 = tb.Length;
                    break;
                default:
                    FieldEdit te = (FieldEdit)advPropertyGrid1.SelectedObject;
                    fd.IsNullable_2 = te.AllowNull;
                    fd.DefaultValue_2 = te.DefaultValue;
                    break;
            };
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        
        #region 字段类型

        /// <summary>
        /// 字段信息显示或编辑
        /// </summary>
        public class FieldEdit
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public FieldEdit()
            {
                AllowNull = true;
            }

            /// <summary>
            /// 获取或设置是否允许为空
            /// </summary>
            [DefaultValue(true)]
            [DisplayName("允许为空")]
            [Browsable(true)]
            [Description("获取或设置是否允许为空")]
            public bool AllowNull { get; set; }

            /// <summary>
            /// 获取或设置默认值
            /// </summary>
            [DefaultValue("")]
            [DisplayName("默认值")]
            [Browsable(true)]
            [Description("获取或设置默认值")]
            public string DefaultValue { get; set; }

        }

        /// <summary>
        /// 字符串
        /// </summary>
        public class FieldString : FieldEdit
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public FieldString()
            {
                Length = 50;
            }

            /// <summary>
            /// 获取或设置长度
            /// </summary>
            [DefaultValue(50)]
            [DisplayName("长度")]
            [Browsable(true)]
            [Description("获取或设置长度")]
            public int Length { get; set; }

        }

        /// <summary>
        /// 大字段
        /// </summary>
        public class FieldBlob
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public FieldBlob()
            {
                AllowNull = true;
                Length = 0;
            }
            /// <summary>
            /// 获取或设置是否允许为空
            /// </summary>
            [DefaultValue(true)]
            [DisplayName("允许为空")]
            [Browsable(true)]
            [Description("获取或设置是否允许为空")]
            public bool AllowNull { get; set; }

            /// <summary>
            /// 获取或设置长度
            /// </summary>
            [DefaultValue(50)]
            [DisplayName("长度")]
            [Browsable(true)]
            [Description("获取或设置长度")]
            public int Length { get; set; }

        }

        /// <summary>
        /// 栅格字段
        /// </summary>
        public class FieldRaster
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public FieldRaster()
            {
                ManagedByGDB = true;
            }
            /// <summary>
            /// 获取或设置you
            /// </summary>
            [DefaultValue("")]
            [DisplayName("描述")]
            [Browsable(true)]
            [Description("获取或设置描述")]
            public string Description { get; set; }

            /// <summary>
            /// 获取或设置长度
            /// </summary>
            [DefaultValue(50)]
            [DisplayName("长度")]
            [Browsable(true)]
            [Description("获取或设置长度")]
            public bool ManagedByGDB { get; set; }

        }
        
        //public class 
        #endregion


    }


}
